/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.becario;

import com.uca.sisbec.commons.SisbecAction;
import com.uca.sisbec.commons.Utils;
import com.uca.sisbec.helpers.becario.IrregularidadHelper;
import com.uca.sisbec.mapping.Becario;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

/**
 *
 * @author Juank
 */
public class IrregularidadAction extends SisbecAction {
    public ActionForward init (ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) {
        if (!isBecarioInSession(request)) return mapping.findForward("searchBecario");
        IrregularidadHelper helper = new IrregularidadHelper(getSession(request));
        Becario becario = getBecarioInSession(request);
        ActionMessages msg = new ActionMessages();
        
        if (!helper.isValidCarnet(becario))
            msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("becario.error.carnet"));
        
        String cum = helper.getCumDeBecario(becario);
        if (!Utils.isNullOrEmpty(cum)){
            cum = (cum.length() > 4) ? cum.substring(0, 4) : cum;
            Double cumNum = new Double(cum);
            if (cumNum < 7){
                msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("becario.error.cum", cum));
            }
        }
        if (helper.isPendienteLibros(becario)){
            msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("becario.error.librosPendientes"));
        }
        
        if (!msg.isEmpty())
            request.setAttribute("mensajes", msg);
        // pendiente el de reuniones
        return mapping.findForward("success");
    }
}
